#region Using directives
using System;
using System.IO;
using SharpWind.Helpers;
using Microsoft.DirectX;
using SharpWind.Resources;
#endregion

namespace SharpWind.Resources.Sound
{
	public class SoundObject : Resource
	{
		#region Fields

		private uint sound = 0;

		private uint buffer = 0;

		#endregion

		#region Methods

		internal override bool Load( string name )
		{
			if( string.IsNullOrEmpty( name ) )
			{
				throw new ArgumentNullException( name );
			}

			this.name	= name;
			this.sound	= FreeSL.fslLoadSound( Path.Combine( Directories.Sounds, name ) );

			return this.sound != 0;
		}

		internal bool Load( string package, string name )
		{
			if( string.IsNullOrEmpty( package ) )
			{
				throw new ArgumentNullException( "package" );
			}

			if( string.IsNullOrEmpty( name ) )
			{
				throw new ArgumentNullException( "name" );
			}

			this.name  = name;
			this.sound = FreeSL.fslLoadSoundFromZip( Path.Combine( Directories.Content, package ), name );

			return this.sound != 0;
		}

		public void Play()
		{
			Play( false );
		}

		public void Play( bool loop )
		{
			FreeSL.fslSoundSetLooping( this.sound, loop );
			FreeSL.fslSoundPlay( this.sound );
		}

		public void Stop()
		{
			FreeSL.fslSoundStop( this.sound );
		}

		public void Pause()
		{
			FreeSL.fslSoundPause( this.sound );
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( this.sound != 0 )
				{
					FreeSL.fslFreeSound( this.sound, true );
				}
			}
		}

		#endregion

		#region Properties

		public bool IsPlaying
		{
			get
			{
				return FreeSL.fslSoundIsPlaying( this.sound );
			}
		}

		public bool IsPaused
		{
			get
			{
				return FreeSL.fslSoundIsPaused( this.sound );
			}
		}

		public bool IsLooping
		{
			get
			{
				return FreeSL.fslSoundIsLooping( this.sound );
			}
		}

		public Vector3 Position
		{
			set
			{
				FreeSL.fslSoundSetPosition( this.sound, value.X, value.Y, value.Z );
			}
		}

		public Vector3 Velocity
		{
			set
			{
				FreeSL.fslSoundSetVelocity( this.sound, value.X, value.Y, value.Z );
			}
		}

		public float Pitch
		{
			set
			{
				FreeSL.fslSoundSetPitch( this.sound, value );
			}
		}

		public float Gain
		{
			set
			{
				FreeSL.fslSoundSetGain( this.sound, value );
			}
		}

		public float MaxDistance
		{
			set
			{
				FreeSL.fslSoundSetMaxDistance( this.sound, value );
			}
		}

		public float ReferenceDistance
		{
			set
			{
				FreeSL.fslSoundSetReferenceDistance( this.sound, value );
			}
		}

		#endregion
	}
}
